var spareId = getQueryString("id");
var producor = getQueryString("producor");
var devType = getQueryString("devType");
$(function () {
    $("#jqGrid").Grid({
        url: '../sparepartdev/listBySpare?spareId=' + spareId,
        colModel: [
            {label: '设备编码', name: 'devCode', index: 'DEV_CODE', width: 80},
            {label: '设备名称', name: 'devName', index: 'DEV_NAME', width: 80},
            {label: '大类', name: 'devTypeB', index: 'DEV_TYPE_B', width: 80},
            {label: '中类', name: 'devTypeM', index: 'DEV_TYPE_M', width: 80},
            {label: '小类', name: 'devTypeS', index: 'DEV_TYPE_S', width: 80},
            {label: '所属车间', name: 'devFarmName', index: 'DEV_FARM_NAME', width: 80},
            {label: '供应厂家', name: 'devSupplyName', index: 'DEV_SUPPLY_NAME', width: 80},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: 'checkFlag', name: 'checkFlag', index: 'CHECK_FLAG', width: 80,hidden: true},
            {
                label: '审核状态', name: 'checkFlagStr', index: 'CHECK_FLAG_STR', width: 80, formatter: function (value, col, row) {
                    var value = row.checkFlag;
                    return sysEnums.CHECK_FLAG[value];
                }
            },
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        sparePartDev: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            devProductName:'',
            devProceName:'',
            devName:''
        },
        deviceList: [],
        deviceInfo: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.sparePartDev = {id: '', delFlag: 1, spareId: spareId, checkFlag: 0};
            vm.getDropDown();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.sparePartDev.id == (null || '') ? "../sparepartdev/save" : "../sparepartdev/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.sparePartDev),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../sparepartdev/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
            /*confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../sparepartdev/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });*/
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../sparepartdev/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.sparePartDev = r.sparePartDev;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData:vm.q,
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                devProductName:'',
                devProceName:'',
                devName:''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getDropDown: function () {
            Ajax.request({
                url: "../deviceinfo/queryAll",
                async: true,
                successCallback: function (r) {
                    vm.deviceList = r.list;
                }
            });
        },
        getByDeviceId: function (item) {
            vm.deviceInfo = item;
            vm.sparePartDev.devId = item.devCode;
        },
        review: function () { //审核
            // 记录必选
            var sparePartDevRecords = getSelectedRowDatas("#jqGrid");
            if(sparePartDevRecords && sparePartDevRecords.length == 0){
                return alert('请选择记录后再进行审核');
            }
            // 已审核过的不允许再次审核
            let sparePartDevRecordIdList = [];
            for (let i = 0; i < sparePartDevRecords.length; i++) {
                if(sparePartDevRecords[i].CHECK_FLAG == "已审核"){
                    return alert('请移除审核过的记录后再进行审核');
                }
                sparePartDevRecordIdList.push(sparePartDevRecords[i].id);
            }
            confirm('确定要审核选中的记录？', function () {
                vm.sparePartDev.checkFlag = 1;
                Ajax.request({
                    url: '../sparepartdev/batchReview',
                    params: JSON.stringify(sparePartDevRecordIdList),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        //显示设备列表-所属设备编码
        getDeviceInfo: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择设备',
                content: '../sys/deviceInfoSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getDeviceInfo();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        var param = {devId:res.id,sparePartIdList:[spareId]};
                        Ajax.request({
                            url: "../sparepartdev/save",
                            params: JSON.stringify(param),
                            type: "POST",
                            contentType: "application/json",
                            successCallback: function (r) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            }
                        });
                    }
                }
            })
        }
    }
});